package com.zxy.ziems.server.controller;

import com.zxy.btp.common.response.JsonResponse;
import com.zxy.btp.common.response.PageResult;
import com.zxy.ziems.server.dto.DeptDto;
import com.zxy.ziems.server.dto.DeptQueryDto;
import com.zxy.ziems.server.dto.DeptTreeNode;
import com.zxy.ziems.server.service.DeptService;
import com.zxy.ziems.server.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


/**
 * @DATE 2021/3/8
 */
@RestController
@RequestMapping("/api/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 新增或修改部门
     *
     * @param deptDto
     * @return
     */
    @PostMapping("/addOrUpdate")
    public JsonResponse<String> addOrUpdate(@RequestBody DeptDto deptDto) {
        deptService.addOrUpdate(deptDto);
        return JsonResponse.SUCCESS();
    }

    /**
     * 查询组织树
     *
     * @return
     */
    @GetMapping("/tree")
    public JsonResponse<List<DeptTreeNode>> tree() {
        DeptTreeNode reslut = deptService.tree();
        return JsonResponse.SUCCESS(Collections.singletonList(reslut));
    }

    /**
     * 分页查询部门列表
     *
     * @param deptQueryDto
     * @return
     */
    @PostMapping("/pageList")
    public JsonResponse<PageResult<DeptVo>> pageList(@RequestBody DeptQueryDto deptQueryDto) {
        PageResult<DeptVo> result = deptService.pageList(deptQueryDto);
        return JsonResponse.SUCCESS(result);
    }

    /**
     * 部门详情
     *
     * @param deptId
     * @return
     */
    @GetMapping("/detail")
    public JsonResponse<DeptVo> detail(@RequestParam String deptId) {
        DeptVo result = deptService.detail(deptId);
        return JsonResponse.SUCCESS(result);
    }

    /**
     * 删除部门
     *
     * @param deptId
     * @return
     */
    @GetMapping("/delete")
    public JsonResponse<String> delete(@RequestParam String deptId) {
        deptService.delete(deptId);
        return JsonResponse.SUCCESS();
    }


}
